package com.wtwd.device.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.wtwd.device.constant.AppMsgEnum;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.constant.SeparatorConstant;
import com.wtwd.device.dao.AppUserInfoMapper;
import com.wtwd.device.model.dto.PushMsgDTO;
import com.wtwd.device.model.entity.AppUserInfo;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.service.AppUserInfoService;
import com.wtwd.device.service.LocaleSourceService;
import com.wtwd.device.service.PushService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zdl
 * @since 2021-08-12
 */
@Service
public class AppUserInfoServiceImpl implements AppUserInfoService {

    @Resource
    private AppUserInfoMapper appUserInfoMapper;

    @Resource
    private PushService pushService;

    @Resource(name = "pushLocaleSourceServiceImpl")
    private LocaleSourceService localeSourceService;

    @Override
    public boolean save(AppUserInfo appUserInfo) {
        return appUserInfoMapper.insert(appUserInfo) == 1;
    }

    @Override
    public boolean update(AppUserInfo appUserInfo) {
        return appUserInfoMapper.updateById(appUserInfo) == 1;
    }

    @Override
    public AppUserInfo getAppUserInfoByUserId(Long userId) {
        return appUserInfoMapper.selectById(userId);
    }

    @Override
    public String getAppUserPushKey(Long userId) {
        AppUserInfo userInfo = new LambdaQueryChainWrapper<>(appUserInfoMapper)
                .eq(AppUserInfo::getUserId, userId)
                .select(AppUserInfo::getClientId)
                .one();
        String clientId;
        if (userInfo == null || StringUtils.isBlank(clientId = userInfo.getClientId())) {
            return null;
        }
        return userId + SeparatorConstant.DOT + clientId;
    }

    @Override
    public String getAppUserLang(Long userId) {
        AppUserInfo userInfo = new LambdaQueryChainWrapper<>(appUserInfoMapper)
                .eq(AppUserInfo::getUserId, userId)
                .select(AppUserInfo::getLang)
                .one();
        return userInfo == null ? null : userInfo.getLang();
    }

    /**
     * 1.获取推送pushkey
     * 2.获取用户语言
     * 3.本地化资源
     *
     * @param toUserId
     * @param clientType
     * @param appMsgEnum
     * @param extraParams
     * @return
     */
    @Override
    public ResultVO pushMsgToAppUser(Long toUserId, String clientType, AppMsgEnum appMsgEnum, String... extraParams) {
        String pushKey = getAppUserPushKey(toUserId);
        String appUserLang = getAppUserLang(toUserId);
        String title = localeSourceService.getMessage(appMsgEnum.getTitleKey(), appUserLang);
        String contents = localeSourceService.getMessage(appMsgEnum.getContentsKey(), appUserLang);
        String extra = appMsgEnum.getExtraParams();
        extra = extraParams.length == 0 ? extra : String.format(extra, extraParams);
        return pushService.pushMsg(new PushMsgDTO(pushKey, title, contents, clientType, extra));
    }

    @Override
    public ResultVO pushTemplateMsgToAppUser(Long toUserId, String clientType, AppMsgEnum appMsgEnum, String templateParam, String... extraParams) {
        String pushKey = getAppUserPushKey(toUserId);
        if (StringUtils.isBlank(pushKey)){
          return ResultVO.error(RespCode.USER_NOT_CLIENTID);
        }
        String appUserLang = getAppUserLang(toUserId);
        String title = localeSourceService.getMessage(appMsgEnum.getTitleKey(), appUserLang);
        String str = localeSourceService.getMessage(appMsgEnum.getContentsKey(), appUserLang);
        String contents = MessageFormat.format(str, templateParam);
        String extra = appMsgEnum.getExtraParams();
        extra = extraParams.length == 0 ? extra : String.format(extra, extraParams);
        return pushService.pushMsg(new PushMsgDTO(pushKey, title, contents, clientType, extra));
    }

}
